 package CurtainDesign;

 import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.FileReader;
 import java.io.FileWriter;
 import java.text.DecimalFormat;

 public class PipeIndexFile
 {
   static int m_MaxPipeFileIndex = 0;

   public String GetPipeIndex(String windowtype, boolean isnextindex)
   {
     String pipeindex = "";
     FileReader fr = null;
     BufferedReader br = null;
     String line = null;
     int linenum;
     if (isnextindex)
       linenum = 1;
     else {
       linenum = 0;
     }
     DecimalFormat format = new DecimalFormat("000000");
     try {
       File file = MaxPipeIndexFile(windowtype);
       if ((file == null) || (!file.exists())) {
         return "";
       }
       fr = new FileReader(file);
       br = new BufferedReader(fr);
       line = br.readLine();
       while (line != null) {
         line = br.readLine();
         linenum++;
       }
       pipeindex = format.format((m_MaxPipeFileIndex - 1) * 3 + linenum);

       br.close();
       fr.close();
     } catch (Exception e) {
       e.printStackTrace();
     }
     return pipeindex;
   }

   public boolean CreatePipeIndexFile(String windowtype)
   {
     String dirpath = Constants.PIPE_PATH;
     File filedir = new File(dirpath);
     if (!filedir.isDirectory()) {
       return false;
     }
     File file = MaxPipeIndexFile(windowtype);
     if (file == null) {
       file = new File(dirpath + "\\" + "流水号记录_1.dat");
     } else if (ExceedMaxPipeNum(file)) {
       String[] strs1 = file.getName().split("\\.");
       String[] strs2 = strs1[0].split("_");
       Integer integer = new Integer(strs2[1]);
       int newindex = integer.intValue() + 1;
       file = null;
       file = new File(dirpath + "\\" + "流水号记录_" + newindex + ".dat");
     }
     else
     {
       return false;
     }
     FileOutputStream outfile = null;
     try {
       file.createNewFile();
       outfile = new FileOutputStream(file);
       outfile.flush();
       outfile.close();
     } catch (Exception e) {
       e.printStackTrace();
     }
     return true;
   }

   public boolean AppendPipeIndexFile(String windowtype, boolean override) {
     FileReader fr = null;
     BufferedReader br = null;
     FileWriter fw = null;
     BufferedWriter bw = null;
     String line = null;
     int linenum = 1;

     if (override);
     DecimalFormat format = new DecimalFormat("000000");
     File file = MaxPipeIndexFile(windowtype);
     if (!file.exists()) {
       return false;
     }
     int ordindex = FindOrderIndex(windowtype, CurtainDesign.m_UserMainGUI.GetOrderInfo().GetOrderIndex());

     if (ordindex > -1)
     {
       String tmp = "";
       tmp = format.format(ordindex);
       CurtainDesign.m_UserMainGUI.GetOrderInfo().SavePipeIndex(tmp);
       return true;
     }
     try {
       fr = new FileReader(file);
       br = new BufferedReader(fr);
       fw = new FileWriter(file, true);
       bw = new BufferedWriter(fw);
       line = br.readLine();
       while (line != null) {
         line = br.readLine();
         linenum++;
       }
       String tmp = "";
       tmp = format.format((m_MaxPipeFileIndex - 1) * 3 + linenum);

       CurtainDesign.m_UserMainGUI.GetOrderInfo().SavePipeIndex(tmp);
       bw.write(CurtainDesign.m_UserMainGUI.GetOrderInfo().GetOrderIndex() + "\n");

       br.close();
       fr.close();
       bw.close();
       fw.close();
     } catch (Exception e) {
       e.printStackTrace();
     }
     return true;
   }

   public File MaxPipeIndexFile(String windowtype)
   {
     String dirpath = Constants.PIPE_PATH;
     File filedir = new File(dirpath);
     if (!filedir.isDirectory()) {
       return null;
     }
     File[] files = filedir.listFiles();
     if ((files == null) || (files.length == 0)) {
       return null;
     }
     String[] names = new String[files.length];
     m_MaxPipeFileIndex = 0;
     File maxfile = files[0];
     if ((files.length == 1) && (!files[0].getName().matches("流水号记录.*"))) {
       return null;
     }

     for (int i = 0; i < names.length; i++) {
       names[i] = files[i].getName();
       if (!names[i].matches("流水号记录.*")) {
         continue;
       }
       String[] strs1 = names[i].split("\\.");
       String[] strs2 = strs1[0].split("_");
       Integer integer = new Integer(strs2[1]);
       if (integer.intValue() > m_MaxPipeFileIndex) {
         m_MaxPipeFileIndex = integer.intValue();
         maxfile = files[i];
       }
     }
     return maxfile;
   }

   public boolean ExceedMaxPipeNum(File file)
   {
     FileReader fr = null;
     BufferedReader br = null;
     String line = null;
     int linenum = 0;

     if (!file.exists())
       return false;
     try
     {
       fr = new FileReader(file);
       br = new BufferedReader(fr);
       line = br.readLine();
       while (line != null) {
         line = br.readLine();
         linenum++;
       }
       br.close();
       fr.close();
     } catch (Exception e) {
       e.printStackTrace();
     }

     return linenum >= 3;
   }

   public int FindOrderIndex(String windowtype, String orderindex)
   {
     FileReader fr = null;
     BufferedReader br = null;
     FileWriter fw = null;
     BufferedWriter bw = null;
     String line = null;
     int linenum = 1;

     String dirpath = Constants.PIPE_PATH;
     File filedir = new File(dirpath);
     if (!filedir.isDirectory()) {
       return -1;
     }
     File[] files = filedir.listFiles();
     if ((files == null) || (files.length == 0)) {
       return -1;
     }
     for (int i = 0; i < files.length; i++) {
       if (!files[i].getName().matches("流水号记录.*")) {
         continue;
       }
       try
       {
         fr = null;
         br = null;
         fw = null;
         fw = null;
         fr = new FileReader(files[i]);
         br = new BufferedReader(fr);
         fw = new FileWriter(files[i], true);
         bw = new BufferedWriter(fw);
         line = br.readLine();
         while (line != null) {
           if (line.equals(orderindex)) {
             return linenum;
           }
           line = br.readLine();
           linenum++;
         }
         br.close();
         fr.close();
         bw.close();
         fw.close();
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
     return -1;
   }
 }

/* Location:           C:\Users\Administrator\Desktop\奥特百叶窗\Shutter DesignerV3.1\CurtainDesign.jar
 * Qualified Name:     CurtainDesign.PipeIndexFile
 * JD-Core Version:    0.6.0
 */